//
//  NewsListBannerView.m
//  ShowYork
//
//  Created by hxh on 2021/5/14.
//  Copyright © 2021 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);

#import "NewsListBannerView.h"
#import "LessonBannerModel.h"
#import "XHPageControl.h"
@interface NewsListBannerView ()<SDCycleScrollViewDelegate>
@property (nonatomic,strong)SDCycleScrollView *scrollView;
@property (nonatomic,strong)XHPageControl *page;
@property (nonatomic,copy)doneBlock done;
@end

@implementation NewsListBannerView

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    

    CGFloat rate = 134.0/343.0;
    CGFloat itemW = kScreenW - 32;
    CGFloat itemH = itemW * rate;
    
    self.frame = CGRectMake(0, 0, kScreenW,itemH + 36);
    
    self.backgroundColor = [UIColor clearColor];
    
    self.scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16,16, kScreenW - 32, itemH) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    self.scrollView.autoScrollTimeInterval = 3;
    self.scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.scrollView.backgroundColor = RGBA(240, 240, 240, 1);
    self.scrollView.showPageControl = NO;
    self.scrollView.layer.cornerRadius = 8;
    self.scrollView.layer.masksToBounds = YES;
    [self addSubview:self.scrollView];
    
    self.page = [[XHPageControl alloc]initWithFrame:CGRectMake(0, self.cl_height - 40, kScreenW, 30)];
    self.page.numberOfPages = 3;
    self.page.currentPage = 0;
    self.page.controlSpacing = 2.0f;
    self.page.controlSize = 4;
    self.page.currentMultiple = 2;
    self.page.otherColor = RGBA(204, 204, 204, 1);
    self.page.currentColor = RGBA(238, 157, 59, 1);
    self.page.hidden = YES;
    
    [self addSubview:self.page];
}

- (void)setItemArray:(NSArray *)itemArray{
    if (itemArray.count == 1) {
        self.scrollView.autoScroll = NO;
    }else{
        self.scrollView.autoScroll = YES;
    }
 
    self.hidden = itemArray.count == 0;
    
    NSMutableArray *urls = [NSMutableArray array];
 
    [itemArray enumerateObjectsUsingBlock:^(LessonBannerModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [urls addObject:obj.image];
    }];
    
    self.page.numberOfPages = itemArray.count;
    
    self.page.hidden = itemArray.count <= 1;
    
    self.scrollView.imageURLStringsGroup = urls;
}

- (void)adjustWhenControllerViewWillAppera{
    [self.scrollView adjustWhenControllerViewWillAppera];
}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    self.done(index);
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    self.page.currentPage = index;
}


@end
